<template>
  <div class="app-container">
    <div class="block">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="listQuery.title" placeholder="标题"></el-input>
        </el-col>
        <el-col :span="6">
          <el-input v-model="listQuery.author" placeholder="作者"></el-input>
        </el-col>
        <el-col :span="8">
          <el-date-picker
            v-model="rangeDate"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="发布起始日期"
            end-placeholder="发布截至日期"
            value-format="yyyyMMddHHmmss"
            align="right"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="4">
          <el-button type="success" icon="el-icon-search" @click.native="search">{{
            $t('button.search')
          }}</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click.native="reset">{{
            $t('button.reset')
          }}</el-button>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col :span="24">
          <el-button type="success" icon="el-icon-plus" @click.native="add">{{
            $t('button.add')
          }}</el-button>
          <el-button type="primary" icon="el-icon-edit" @click.native="edit">{{
            $t('button.edit')
          }}</el-button>
          <el-button type="danger" icon="el-icon-delete" @click.native="remove">{{
            $t('button.delete')
          }}</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="ID">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="标题">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="发布日期">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="作者">
        <template slot-scope="scope">
          {{ scope.row.author }}
        </template>
      </el-table-column>
      <el-table-column label="文章配图">
        <template slot-scope="scope">
          <img :src="scope.row.img" style="width: 200px" />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100, 500]"
      :page-size="listQuery.limit"
      :total="total"
      @size-change="changeSize"
      @current-change="fetchPage"
      @prev-click="fetchPrev"
      @next-click="fetchNext"
    >
    </el-pagination>
  </div>
</template>
<script>
import { remove, getList } from '@/api/cms/article';

import { getApiUrl } from '@/utils/utils';

export default {
  data() {
    return {
      formVisible: false,
      formTitle: '添加文章',
      deptList: [],
      isAdd: true,
      form: {
        id: '',
        title: '',
        author: '',
        img: '',
      },

      listQuery: {
        page: 1,
        limit: 20,
        title: undefined,
        author: undefined,
        startDate: undefined,
        endDate: undefined,
      },
      rangeDate: undefined,
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            },
          },
        ],
      },
    };
  },
  computed: {
    rules() {
      return {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        author: [{ required: true, message: '作者不能为空', trigger: 'blur' }],
      };
    },
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      let queryData = this.listQuery;
      if (this.rangeDate) {
        queryData['startDate'] = this.rangeDate[0];
        queryData['endDate'] = this.rangeDate[1];
      }
      getList(queryData).then((response) => {
        this.list = response.data.records;
        for (var index in this.list) {
          const item = this.list[index];
          item.img = getApiUrl() + '/file/getImgStream?idFile=' + item.img;
        }
        this.listLoading = false;
        this.total = response.data.total;
      });
    },
    search() {
      this.fetchData();
    },
    reset() {
      this.listQuery.title = undefined;
      this.listQuery.author = undefined;
      this.listQuery.startDate = undefined;
      this.listQuery.endDate = undefined;
      this.rangeDate = '';
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.page = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.limit = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow) {
      this.selRow = currentRow;
    },
    add() {
      this.$router.push({ path: '/cms/article/edit' });
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true;
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning',
      });
      return false;
    },
    edit() {
      if (this.checkSel()) {
        this.$router.push({ path: '/cms/article/edit', query: { id: this.selRow.id } });
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id;

        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning',
        })
          .then(() => {
            remove(id).then(() => {
              this.$message({
                message: this.$t('common.optionSuccess'),
                type: 'success',
              });
              this.fetchData();
            });
          })
          .catch(() => {});
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import '~@/styles/common.scss';
</style>
